/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.event;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import dgbgui.actions.ActionProxy;
import dgbgui.actions.ActionRepository;
import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.WidgetStore;
import dgbgui.gui.widgets.visual.QueryWidget;
import dgbgui.gui.widgets.visual.TitleBarWidget;
import dgbgui.gui.widgets.visual.definition.DefinitionWidget;
import dgbgui.gui.widgets.visual.results.ResultWidget;
import dgbgui.modeldocument.Query;
import dgbgui.selection.SelectionManager;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

public class DoubleClickDefinitionEventHandler extends PBasicInputEventHandler implements
        CheckConsumedInputEventHandler {

    public boolean eventProcessed(PInputEvent event, int type) {
        m_processed = false;
        processEvent(event, type);
        return m_processed;
    }
    
    public void mousePressed(PInputEvent event) {
        if ( event.getClickCount() == 2 ) {
            m_processed = true;
            
            // If the item that was double-clicked is a filter definition
            // widget, launch the edit properties tool, but first set the 
            // selection to the query that owns the definition
            
            LinkedHashSet<Class> objectPreference = new LinkedHashSet<Class>();
            objectPreference.add(DefinitionWidget.class);
            objectPreference.add(TitleBarWidget.class);
            objectPreference.add(ResultWidget.class);
            
            Widget preferredObj = Widget.findPreferencialObject(
                    (event.getCamera().pick(
                            event.getCanvasPosition().getX(),
                            event.getCanvasPosition().getY(),
                            1)).getNodeStackReference(),
                            objectPreference);

            if ( preferredObj != null ) { 
                if ( preferredObj instanceof DefinitionWidget ) { 
                    QueryWidget widget = (QueryWidget)preferredObj.getParentOfClass(QueryWidget.class);
                    Query query = WidgetStore.getInstance().getQuery(widget);
                    // Could be a nested query, and thus not editable
                    if ( query != null ) {
                        Set<Object> querySet = new HashSet<Object>();
                        querySet.add(query);
                        SelectionManager.getInstance().setSet(Query.class,querySet);

                        // Launch the properties action if it is activae
                        if ( ActionRepository.getInstance().getAction(ActionRepository.ActionName.EDIT_MODIFY).isEnabled() ) {
                            ActionProxy.getInstance().actionInitiated(ActionRepository.ActionName.EDIT_MODIFY);
                        }
                    }
                    m_processed = true;
                }
                if ( preferredObj instanceof TitleBarWidget ) { 
                    // Launch the properties action if it is activae
                    if ( ActionRepository.getInstance().getAction(ActionRepository.ActionName.EDIT_RENAME).isEnabled() ) {
                        ActionProxy.getInstance().actionInitiated(ActionRepository.ActionName.EDIT_RENAME);
                    }
                    m_processed = true;
                }
                
                if ( preferredObj instanceof ResultWidget ) { 
                    // Launch the properties action if it is activae
                    if ( ActionRepository.getInstance().getAction(ActionRepository.ActionName.QUERY_CREATE_SET).isEnabled() ) {
                        ActionProxy.getInstance().actionInitiated(ActionRepository.ActionName.QUERY_CREATE_SET);
                    }
                    m_processed = true;
                }
            }
        }
    }
    


    private boolean m_processed;
}
